package com.lbt.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbt.commonuntils.R;
import com.lbt.eduorder.entity.Order;
import com.lbt.eduorder.service.OrderService;
import com.lbt.eduorder.service.PayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-09
 */
//@CrossOrigin
@RestController
@Api(description = "订单支付")
@RequestMapping("/eduorder/paylog")
public class PayLogController {

    @Autowired
    PayLogService payLogService;

    @Autowired
    OrderService orderService;

    @ApiOperation("生成订单二维码")
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable("orderNo")String orderNo){

        Map map=payLogService.createNative(orderNo);
        return R.ok().data(map);
    }

    @ApiOperation("查询支付状态，生成支付日志")
    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable("orderNo")String orderNo){
        //根据订单号查询支付的状态
        Map<String,String> map=payLogService.queryPayStatus(orderNo);
        if(map==null){
            return R.error().message("支付出错了");
        }
        //调试
    //    System.out.println("查询支付状态："+map.toString());
        //如果map 有信息
        if(map.get("trade_state").equals("SUCCESS")){//支付成功
            //  添加记录到记录表  更新订单表中的状态
            payLogService.updateOrderStatus(map);
          //  System.out.println("查询支付状态*****************支付成功//////:支付状态"+map.get("trade_state")+"***************");
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}

